package com.edais.util.sina;

import java.net.URLEncoder;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edais.util.CommonUtil;
import com.edais.util.JsonUtil;
import com.edais.util.ThirdPartyEnvUtil;
import com.edais.util.RSAThirdParty.RSA;

public class ThridPartyFunctionUtil {
	
	private static final Log logger = LogFactory.getLog(ThridPartyFunctionUtil.class);
	
	public static Map<String,String> unBindingVerify(Map<String, String> paramMap){
		String url = ThirdPartyEnvUtil.getValue("unBindingVerifyUrl");

		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}
		logger.debug("sina解绑认证信息请求参数："+requestParam);
		
		String unBindingVerify =  CommonUtil.sendPostByHttp(url, requestParam);
		
		if( null == unBindingVerify ){
			logger.error("sina解绑认证信息失败");
			return null;
		}else{
			//解析发送结果
			Map<String,String> unBindingVerifyMap = JsonUtil.getMapFromJsonString(unBindingVerify);
			return unBindingVerifyMap;
		}
	}
	
	public static Map<String,String> bindingVerify(Map<String,String> paramMap){
		String url = ThirdPartyEnvUtil.getValue("bindingVerifyUrl");
		
		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}
		logger.debug("sina绑定认证信息请求参数："+requestParam);
		
		String bindingVerify = CommonUtil.sendPostByHttp(url, requestParam);
		
		if(null == bindingVerify){
			logger.error("sina绑定认证信息失败");
			return null;
		}else{
			//解析发送结果
			Map<String,String> bindingVerifyMap = JsonUtil.getMapFromJsonString(bindingVerify);
			return bindingVerifyMap;
		}
	}
	
	public static Map<String,String> bankCardUnbind(Map<String, String> paramMap){
		String url = ThirdPartyEnvUtil.getValue("thirdPartyPath");
		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		logger.info("支付-解绑银行卡参数："+paramMap);
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.info("公钥加密串："+e);
		}
		String bankCardUnbindResult = null;
		try {
			bankCardUnbindResult = CommonUtil.sendPostByHttp(url+"/route/unbindingCard", requestParam);
			logger.info("支付-解绑银行卡返回结果："+bankCardUnbindResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if( null == bankCardUnbindResult ){
			logger.error("支付-解绑银行卡失败");
			throw new RuntimeException("支付-解绑银行卡失败");
		}else{
			//解析返回结果
			Map<String,String> bankCardUnbindResultMap = JsonUtil.getMapFromJsonString(bankCardUnbindResult);
			if(null == bankCardUnbindResultMap  || "sysException".equals(bankCardUnbindResultMap.get("rescode"))){
				// 系统异常
				logger.error("支付-解绑银行卡失败:" + String.valueOf(bankCardUnbindResultMap.get("resmsg_cn")));
				throw new RuntimeException("支付-解绑银行卡失败");
			}else if("BusinessException".equals(bankCardUnbindResultMap.get("rescode"))){
				// 业务异常，把错误信息提示给用户
				logger.error("支付-解绑银行卡失败:" + String.valueOf(bankCardUnbindResultMap.get("resmsg_cn")));
				throw new RuntimeException(String.valueOf(bankCardUnbindResultMap.get("resmsg_cn")));
			}
			return bankCardUnbindResultMap;
		}
	}
	
}
